//
//  WmcDateEx.swift
//  YPWatermarkCamera
//
//  Created by 马涛 on 2022/6/27.
//

import Foundation
import Kronos

// MARK: - Date (Operator Overloading)

extension Date {
    /// 日期的加法
    static func + (lhs: Self, rhs: Self) -> TimeInterval {
        return lhs.timeIntervalSince1970 + rhs.timeIntervalSince1970
    }

    /// 日期减法  https://www.appcoda.com.tw/operator-overloading-swift/)
    static func - (lhs: Self, rhs: Self) -> TimeInterval {
        return lhs.timeIntervalSince1970 - rhs.timeIntervalSince1970
    }
    
    /// 当前时间
    static var wmc_netDate: Date {
        if let date = Clock.now {
            return date
        }
        return Date()
    }
    /// 整型时间戳
    var int_timestamp: Int {
        return timeIntervalSince1970.yp.int
    }
    /// 时间戳
    var timestamp: Double {
        return timeIntervalSince1970
    }
    /// 日期转字符串
    func wmc_toString(format: String) -> String {
        let dateFormat = DateFormatter()
        dateFormat.locale = .current
        dateFormat.dateFormat = format
        return dateFormat.string(from: self)
    }

}

// MARK: - Date (class function)

extension Date {
    func _component(_ component: Calendar.Component = .day, for calendar: Calendar = .current) -> Int {
        return calendar.component(component, from: self)
    }

    func _components(_ components: [Calendar.Component] = [.hour, .minute, .second], for calendar: Calendar = .current) -> [Int] {
        let result = components.map { component in
            return self._component(component, for: calendar)
        }

        return result
    }
}
